package arkanoid.components.powers;

import java.awt.Color;

import arkanoid.components.Palette;
import arkanoid.scenes.Level;

import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.GameComponent;
import com.uqbar.vainilla.colissions.CollisionDetector;

public abstract class PowerUp extends GameComponent<Level> {

	private boolean isAvailable = true;
	private int width           = 15;
	private int height          = 15;
	private int speed           = 50;
	private Color color;
	
	private static int GAME_WIDTH;
	
	public void onSceneActivated() {
		GAME_WIDTH = this.getGame().getDisplayWidth();
	}
	
	public abstract void unleash();
	
	public void update(DeltaState deltaState) {
		double advanced = deltaState.getDelta() * this.getSpeed();
		this.move(0, advanced);
		
		Palette palette = this.getScene().getPalette();
		if (this.collidesAgainstPalette(palette)) {
			this.unleash();
			this.setAvailable(true);
			this.destroy();
		}
		
		if (this.getY() > GAME_WIDTH - this.getHeight()) {
			this.setAvailable(true);
		}
	}
	
	private boolean collidesAgainstPalette(Palette palette) {
		return CollisionDetector.INSTANCE.collidesRectAgainstRect(
				this.getX(), this.getY(), this.getWidth(), this.getHeight(),
				palette.getX(), palette.getY(), palette.getWidth(), palette.getHeight()
				);
	}

	
	
	
	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
	}

	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}

	public int getSpeed() {
		return speed;
	}

	public boolean isAvailable() {
		return isAvailable;
	}

	public void setAvailable(boolean isAvailable) {
		this.isAvailable = isAvailable;
	}
}
